package com.example.wearing.tools;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wearing.R;
import com.example.wearing.javabean.Post;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class PostAdapter extends ArrayAdapter {
    private static final String TAG = "PostAdapter";
    public PostAdapter(Context context,
                       int resource,
                       ArrayList<Post> list) {
        super(context, resource, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = LayoutInflater.from(getContext()).inflate(R.layout.post_list,
                    parent,
                    false);
        }

        Post post = (Post) getItem(position);
        TextView title = (TextView) itemView.findViewById(R.id.postTitle);
        TextView username = (TextView) itemView.findViewById(R.id.postUserName);
        ImageView postimg = (ImageView) itemView.findViewById(R.id.postImg);
        TextView time = (TextView) itemView.findViewById(R.id.postTime);
        TextView likeNum = (TextView) itemView.findViewById(R.id.likeNum);
        TextView replyNum = (TextView) itemView.findViewById(R.id.replyNum);
        ImageView userimg = (ImageView) itemView.findViewById(R.id.userImg);


        title.setText(post.getP_title());
        username.setText(post.getP_user());
        time.setText(post.getP_time());
        likeNum.setText(String.valueOf(post.getP_likeNum()));
        replyNum.setText(String.valueOf(post.getP_replyNum()));

        String imgStr = post.getP_img();
        Bitmap bitmap = ImageUtil.base64ToImage(imgStr);
        postimg.setImageBitmap(bitmap);
        String userimgStr = post.getP_user_img();
        Bitmap userimgBitmap = ImageUtil.base64ToImage(userimgStr);
        userimg.setImageBitmap(userimgBitmap);

        return itemView;
    }

}
